/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-11
 * Time: 19:54
 */
//978.最长湍流子数组
public class Solution6 {
    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        //1.创建dp表
        //f[i]表示以i为结尾呈现"上升"趋势的最长湍流子数组长度
        //g[i]表示以i为结尾呈现"下降"趋势的最长湍流子数组长度
        int[] f = new int[n];
        int[] g  = new int[n];
        //2.初始化
        for(int i = 0; i < n; i++) {
            f[i] = 1;
            g[i] = 1;
        }
        //3.状态转移
        //4.填表顺序
        int ret1 = 1;
        int ret2 = 1;
        for(int i = 1; i < n; i++) {
            if(arr[i-1] < arr[i]) { //上升
                f[i] = g[i-1]+1;
            }
            if(arr[i-1] > arr[i]) { //下降
                g[i] = f[i-1]+1;
            }
            ret1 = Math.max(ret1,f[i]);
            ret2 = Math.max(ret2,g[i]);
        }
        return Math.max(ret1,ret2);
    }
}